package com.hmi.simulatedrive.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.help.Tip;
import com.hmi.simulatedrive.R;
import com.hmi.simulatedrive.databinding.ItemLayoutAmapTipBinding;

import java.util.ArrayList;
import java.util.List;

public class AMapTipAdapter extends RecyclerView.Adapter<AMapTipAdapter.MyViewHolder> {
    private Context mContext;
    private List<Tip> tipList;
    private OnItemClickListener itemClickListener;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public AMapTipAdapter(Context mContext, List<Tip> tipList) {
        this.mContext = mContext;
        this.tipList = tipList;
        if (tipList == null) {
            this.tipList = new ArrayList<>();
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_layout_amap_tip, parent);
        ItemLayoutAmapTipBinding itemLayoutAmapTipBinding = ItemLayoutAmapTipBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(itemLayoutAmapTipBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Tip currentTip = tipList.get(position);
        holder.bindView(currentTip.getName(), currentTip.getAddress());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 用户点击了viewHolder
                if (itemClickListener!=null) {
                    itemClickListener.onItemClick(position, v, tipList.get(position));
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return tipList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvAddress;

        public MyViewHolder(ItemLayoutAmapTipBinding itemLayoutAmapTipBinding) {
            super(itemLayoutAmapTipBinding.getRoot());
            this.tvName = itemLayoutAmapTipBinding.tvName;
            this.tvAddress = itemLayoutAmapTipBinding.tvAddress;
        }

        public void bindView(String name, String address) {
            this.tvName.setText(name);
            this.tvAddress.setText(address);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View rootView, Tip tip);
    }
}
